package top.fkxuexi.smartwork;

import freemarker.template.TemplateException;
import top.fkxuexi.smartwork.MetaEntity.TableMeta;
import top.fkxuexi.smartwork.config.SmkConfig;
import top.fkxuexi.smartwork.core.MakeCode;
import top.fkxuexi.smartwork.core.MetaParse;
import top.fkxuexi.smartwork.core.MySQLMetaParse;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class CodeGener {

    private SmkConfig config;
    private MetaParse parse ;
    private MakeCode makeCode;

    public CodeGener(SmkConfig config){
        this.config = config;
        parse = new MySQLMetaParse(config);

    }

    /**
     *  根据指定的表和模板来生成代码
     * @param tables
     * @throws IOException
     * @throws TemplateException
     * @throws SQLException
     */
    public void generTables(String[] tables) throws IOException, TemplateException, SQLException {
        List<TableMeta> tableMeta = parse.getTableMeta(tables);
        makeCode = new MakeCode(config,tableMeta);
        makeCode.makeCode();
    }

    /**
     *
     * @return 返回所有的数据库的表名，用于web项目中的选择表来进行生成
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public List<String> getAllTables() throws SQLException, ClassNotFoundException {
        return parse.getAllTable();
    }


}
